using System;
using System.Collections.Generic;
using System.Text;

namespace codegen.template
{
    public class TObjFunction
    {


        #region TFunctionGeneric Members

        public static void GetObjectByID(string className, string classFileName, string InputName,string[] OutputList)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<?php	" + Environment.NewLine);
            
            sb.Append("/*****************************************************************************\\" + Environment.NewLine);
            sb.Append("" + Environment.NewLine);
            sb.Append("PHP \"jeegoon.com object function \" library" + Environment.NewLine);
            sb.AppendFormat("@function:Get{0}ByID" + Environment.NewLine, className);
            sb.AppendFormat("@version: 0.1 - {0}" + Environment.NewLine, DateTime.Now.ToShortDateString());
            sb.Append("@author: Seraph Jiang - http://www.jeegoon.com/wordpress/" + Environment.NewLine);
            sb.Append("\\*****************************************************************************/" + Environment.NewLine);

            
            sb.AppendFormat("	require_once(\"class.{0}.php\");	" + Environment.NewLine, classFileName);
            sb.Append("	require_once(\"class.db.jeegoon.php\");	" + Environment.NewLine);
            sb.Append("		" + Environment.NewLine);
            sb.AppendFormat("	function Get{0}ByID(${1})	" + Environment.NewLine,className,InputName);
            sb.Append("	{	" + Environment.NewLine);
            sb.AppendFormat("	  if(is_string(${0}))	" + Environment.NewLine,InputName);
            sb.Append("	  {	" + Environment.NewLine);
            sb.Append("	    try	" + Environment.NewLine);
            sb.Append("	    {	" + Environment.NewLine);
            sb.AppendFormat("	      $sh1 = new jeegoon();	" + Environment.NewLine);
            sb.AppendFormat("	      $result = $sh1->query(\"select * from {0} where ID='\" . ${1}  . \"';\");	" + Environment.NewLine,classFileName,InputName);
            sb.Append("	      	" + Environment.NewLine);
            sb.Append("	      $arr=array();	" + Environment.NewLine);
            sb.Append("	      $i=0;	" + Environment.NewLine);
            sb.Append("	      while ($line = mysql_fetch_array($result, MYSQL_BOTH)) {	" + Environment.NewLine);
            sb.Append("	      	" + Environment.NewLine);
            sb.Append("	        $arr[$i]=$line;	" + Environment.NewLine);
            sb.Append("	      $i++;	" + Environment.NewLine);
            sb.Append("	      }	" + Environment.NewLine);
            sb.AppendFormat("	      $mb = new {0}();	" + Environment.NewLine,className);

            for (int i = 0; i < OutputList.Length; i++)
            {
                sb.AppendFormat("	      $mb->{0} = $arr[0][{1}];	" + Environment.NewLine, OutputList[i], i);
            }
            
            sb.Append("		" + Environment.NewLine);
            sb.Append("	      return $mb;	" + Environment.NewLine);
            sb.Append("	    }	" + Environment.NewLine);
            sb.Append("	    catch(Exception $e)	" + Environment.NewLine);
            sb.Append("	    {	" + Environment.NewLine);
            sb.AppendFormat("	      return new soap_falut('client','','Get{0}ByID Error!');	" + Environment.NewLine,className);
            sb.Append("		" + Environment.NewLine);
            sb.Append("	    }	" + Environment.NewLine);
            sb.Append("	  }	" + Environment.NewLine);
            sb.Append("	  else	" + Environment.NewLine);
            sb.Append("	  {	" + Environment.NewLine);
            sb.AppendFormat("	    return new soap_falut('client','','Get{0}ByID Error!');	" + Environment.NewLine,className);
            sb.Append("	  } 	" + Environment.NewLine);
            sb.Append("	}	" + Environment.NewLine);
            sb.Append("?>");

            string text = sb.ToString();
            utility.TextFile.Write(string.Format("class.{0}.get{0}byid.php", classFileName), text);

        }

        public static void GetObjectByName(string className, string[] OutputList)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public static void SetObject(string className,string classFileName ,string[] InputList, string[] OutputList)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<?php	" + Environment.NewLine);

            sb.Append("/*****************************************************************************\\" + Environment.NewLine);
            sb.Append("" + Environment.NewLine);
            sb.Append("PHP \"jeegoon.com object function \" library" + Environment.NewLine);
            sb.AppendFormat("@function:Get{0}ByID" + Environment.NewLine, className);
            sb.AppendFormat("@version: 0.1 - {0}" + Environment.NewLine, DateTime.Now.ToShortDateString());
            sb.Append("@author: Seraph Jiang - http://www.jeegoon.com/wordpress/" + Environment.NewLine);
            sb.Append("\\*****************************************************************************/" + Environment.NewLine);


            sb.AppendFormat("	  require_once(\"class.{0}.php\");	" + Environment.NewLine,classFileName);
            sb.Append("	  require_once(\"class.db.jeegoon.php\");	" + Environment.NewLine);
            sb.Append("		" + Environment.NewLine);
            sb.AppendFormat("	  function Set{0}($p_{0},$p_Action)	" + Environment.NewLine,className);
            sb.Append("	  {	" + Environment.NewLine);
            sb.AppendFormat("	    if(null!=$p_{0})	" + Environment.NewLine,className);
            sb.Append("	    {	" + Environment.NewLine);
            sb.Append("	      try	" + Environment.NewLine);
            sb.Append("	      {	" + Environment.NewLine);
            sb.Append("	        $sh2 = new jeegoon();	" + Environment.NewLine);
            sb.Append("		" + Environment.NewLine);
            sb.Append("	        if(null!=$p_Action)	" + Environment.NewLine);
            sb.Append("	        {	" + Environment.NewLine);
            sb.Append("	          if(3 == $p_Action)	" + Environment.NewLine);
            sb.Append("	          {	" + Environment.NewLine);
            sb.AppendFormat("	            $sql=\"delete from {0} where ID='\" . $p_{1}[\"ID\"] . \"';\";	" + Environment.NewLine,classFileName,className);
            sb.Append("	            $sh2->query($sql);	" + Environment.NewLine);
            sb.Append("	            $sh2->query('commit');	" + Environment.NewLine);
            sb.AppendFormat("	            $usr = new {0}();	" + Environment.NewLine,className);
            sb.AppendFormat("	            $usr->ID =$p_{0}[\"ID\"];	" + Environment.NewLine,className);
            sb.Append("	    	" + Environment.NewLine);
            sb.Append("	            return $usr;	" + Environment.NewLine);
            sb.Append("	          }	" + Environment.NewLine);
            sb.Append("	          else if (1 == $p_Action)	" + Environment.NewLine);
            sb.Append("	          {	" + Environment.NewLine);
            sb.AppendFormat("	            $sql=\"select * from {0} where Name='\" . $p_{1}[\"Name\"] . \"';\";	" + Environment.NewLine,classFileName,className);
            sb.Append("	            $count = $sh2->querycount($sql);	" + Environment.NewLine);
            sb.Append("	            	" + Environment.NewLine);
            sb.Append("	            if ($count==0)	" + Environment.NewLine);
            sb.Append("	            {	" + Environment.NewLine);

            sb.AppendFormat("	              $sql1 = \"insert into {0}\";" + Environment.NewLine,classFileName);
            
            sb.AppendFormat("	              $sql1 = $sql1 . \"({0}\";" + Environment.NewLine,InputList[0]);
            for(int i =1;i<InputList.Length-1;i++)
            {
                sb.AppendFormat("	              $sql1 = $sql1 . \",{0}\";" + Environment.NewLine,InputList[i]);
            }
            sb.AppendFormat("	              $sql1 = $sql1 . \",{0}) \";" + Environment.NewLine, InputList[InputList.Length - 1]);

            sb.Append("	              $sql1 = $sql1 . \"values (\";" + Environment.NewLine);

            for (int i = 0; i < InputList.Length - 1; i++)
            {
                sb.AppendFormat("	              $sql1 = $sql1 . \"'\" . $p_{0}[\"{1}\"] . \"',\" ;	" + Environment.NewLine, className, InputList[i]);
            }
            sb.AppendFormat("	              $sql1 = $sql1 . \"'\" . $p_{0}[\"{1}\"] . \"');\" ;	" + Environment.NewLine, className, InputList[InputList.Length-1]);

            sb.Append("	              $result = $sh2->query($sql1);	" + Environment.NewLine);
            sb.Append("	              $result = $sh2->query('commit');	" + Environment.NewLine);
            
            sb.Append("	    	" + Environment.NewLine);
            sb.Append("	              try	" + Environment.NewLine);
            sb.Append("	              {	" + Environment.NewLine);
            sb.Append("	                $sh1 = new jeegoon();	" + Environment.NewLine);
            sb.Append("	                $result = $sh1->query($sql);	" + Environment.NewLine);
            sb.Append("	                	" + Environment.NewLine);
            sb.Append("	                $arr=array();	" + Environment.NewLine);
            sb.Append("	                $i=0;	" + Environment.NewLine);
            sb.Append("	                while ($line = mysql_fetch_array($result, MYSQL_BOTH)) {	" + Environment.NewLine);
            sb.Append("	                  $arr[$i]=$line;	" + Environment.NewLine);
            sb.Append("	                  $i++;	" + Environment.NewLine);
            sb.Append("	                }	" + Environment.NewLine);
            sb.Append("	                	" + Environment.NewLine);
            sb.Append("	                	" + Environment.NewLine);
            sb.AppendFormat("	                $mb = new {0}();	" + Environment.NewLine,className);


            for (int i = 0; i < OutputList.Length; i++)
            {
                sb.AppendFormat("	                $mb->{0} = $arr[0][{1}];	" + Environment.NewLine, OutputList[i], i);
            }

            sb.Append("	                return $mb;	" + Environment.NewLine);

            sb.Append("	              }	" + Environment.NewLine);
            sb.Append("	              catch(Exception $e)	" + Environment.NewLine);
            sb.Append("	              {	" + Environment.NewLine);
            sb.AppendFormat("	                return new soap_falut('client','','Set{0} Error!');	" + Environment.NewLine,className);
            sb.Append("	              	" + Environment.NewLine);
            sb.Append("	              }	" + Environment.NewLine);
            sb.Append("	            }	" + Environment.NewLine);
            sb.Append("	          }	" + Environment.NewLine);
            sb.Append("	        }	" + Environment.NewLine);
            sb.Append("	      }	" + Environment.NewLine);
            sb.Append("	      catch(Exception $e)	" + Environment.NewLine);
            sb.Append("	      {	" + Environment.NewLine);
            sb.AppendFormat("	        return new soap_falut('client','','Set{0} Error!');	" + Environment.NewLine,className);
            sb.Append("	      }	" + Environment.NewLine);
            sb.Append("	    }	" + Environment.NewLine);
            sb.Append("	    else	" + Environment.NewLine);
            sb.Append("	    {	" + Environment.NewLine);
            sb.AppendFormat("	      return new soap_falut('client','','Set{0} Error!');	" + Environment.NewLine,className);
            sb.Append("	    }	" + Environment.NewLine);
            sb.Append("	  }	" + Environment.NewLine);
            sb.Append("?>");

            string text = sb.ToString();
            utility.TextFile.Write(string.Format("class.{0}.set{0}.php", classFileName), text);
        }

        #endregion
    }
}
